package TCP;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
    public static void main(String[] args) throws Exception {
        //创建一个socket服务端
        ServerSocket serverSocket = new ServerSocket(8888);
        System.out.println("服务端已启动!");
        //接受消息,如果客户端发送了消息，accept会立马收到一个socket，里面装的是数据
        Socket socket = null;
        InputStream is = null;
        while (true) {
            socket = serverSocket.accept();
            //读取socket中的数据
            is = socket.getInputStream();
            byte[] b = new byte[1024];
            int len;
            while ((len = is.read(b)) != -1) {
                System.out.println("客户端" + socket.getInetAddress() + "说：");
                System.out.println(new String(b, 0, len));
            }
        }

       /* is.close();
        socket.close();*/

    }
}
